﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Zhaoxi.FactoryDeviceSystem.UControls
{
    public partial class USwitch : UserControl
    {
        public USwitch()
        {
            InitializeComponent();
            SetStyle(ControlStyles.Selectable, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);//忽略窗口消息，减少闪烁
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);//绘制到缓冲区，减少闪烁
            SetStyle(ControlStyles.UserPaint, true);//控件由其自身而不是操作系统绘制
            SetStyle(ControlStyles.ResizeRedraw, true);//控件调整其大小时重绘
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);//支持透明背景
            this.BackColor = Color.Transparent;
            MouseDown += USwitch_MouseDown;
        }

        [Description("开关状态改变事件"), Category("自定义")]
        public event EventHandler CheckedChanged;//开关状态改变事件



        private Color m_trueColor = Color.FromArgb(34, 163, 169);

        [Description("打开状态颜色"), Category("自定义")]
        public Color TrueColor
        {
            get { return m_trueColor; } 
            set
            {
                m_trueColor = value;
                Invalidate();
            }
        }

        private Color m_falseColor = Color.FromArgb(111, 122, 126);

        [Description("关闭状态颜色"), Category("自定义")]
        public Color FalseColor
        {
            get { return m_falseColor; }
            set
            {
                m_falseColor = value;
                Invalidate();
            }
        }

        private bool m_checked;
        [Description("开关状态"), Category("自定义")]
        public bool Checked
        {
            get { return m_checked; }
            set
            {
                m_checked = value;
                Invalidate();
            }
        }


        private string[] m_texts;

        [Description("文本值，当开或关的状态文本，必须是长度为2的数组"), Category("自定义")]
        public string[] StateTexts
        {
            get { return m_texts; }
            set
            {
                m_texts = value;
                Invalidate();
            }
        }

        //按下时
        private void USwitch_MouseDown(object sender, MouseEventArgs e)
        {
             CheckedChanged?.Invoke(this, new EventArgs());
        }

        //重绘---椭圆形样式
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;//绘图对象           
            g.SmoothingMode = SmoothingMode.HighQuality;  //呈现质量--高质量呈现 

            //背景色
            var bgColor = m_checked ? m_trueColor : m_falseColor;
            //形状路径
            GraphicsPath path= new GraphicsPath();
            //上边线
            path.AddLine(new Point(Height/2,1),new Point(Width-Height/2,1));
            //右边半圆弧
            path.AddArc(new Rectangle(Width - Height - 1, 1, Height - 2, Height - 2), -90, 180);
            //下边线
            path.AddLine(new Point(Width - Height / 2, Height-1),new Point(Height / 2, Height - 1));
            //左边半圆弧
            path.AddArc(new Rectangle(1, 1, Height - 2, Height - 2), 90, 180);
            //填充路径 
            g.FillPath(new SolidBrush(bgColor), path);

            //文本
            string text = string.Empty;
            if(m_texts!=null &&m_texts.Length==2)
            {
                if (m_checked)
                    text = m_texts[0];
                else 
                    text = m_texts[1];
            }

            if(m_checked)//开的外观
            {
                //右边大圆，左边文本   
                //圆的直径：Height-2-4
                g.FillEllipse(Brushes.White, new Rectangle(Width - Height - 1 + 2, 1 + 2, Height - 2 - 4, Height - 2 - 4));
                if(!string.IsNullOrEmpty(text))//画文本
                {
                    //文本的尺寸
                    SizeF fontSize = g.MeasureString(text.Replace(' ', 'A'), Font);
                    int intTextY = (Height - (int)fontSize.Height) / 2 + 2;//左上角的y值
                    g.DrawString(text,Font,Brushes.White,new Point((Height-2-4)/2,intTextY));//画文本  1/2半径处
                }
            }
            else //关的外观
            {
                //左边大圆，右边文本   
                //圆的直径：Height-2-4
                g.FillEllipse(Brushes.White, new Rectangle(1+2, 1 + 2, Height - 2 - 4, Height - 2 - 4));
                if (!string.IsNullOrEmpty(text))//画文本
                {
                    //文本的尺寸
                    SizeF fontSize = g.MeasureString(text.Replace(' ', 'A'), Font);
                    int intTextY = (Height - (int)fontSize.Height) / 2 + 2;//左上角的y值
                    g.DrawString(text, Font, Brushes.White, new Point(Width-2-Height/2-((int)fontSize.Width)+4, intTextY));//画文本   宽-2-右边半径宽-文字的宽度+4
                }
            }
        }

    }
}
